"use strict";

function asyncGeneratorStep(gen, resolve, reject, _next, _throw, key, arg) { try { var info = gen[key](arg); var value = info.value; } catch (error) { reject(error); return; } if (info.done) { resolve(value); } else { Promise.resolve(value).then(_next, _throw); } }

function _asyncToGenerator(fn) { return function () { var self = this, args = arguments; return new Promise(function (resolve, reject) { var gen = fn.apply(self, args); function _next(value) { asyncGeneratorStep(gen, resolve, reject, _next, _throw, "next", value); } function _throw(err) { asyncGeneratorStep(gen, resolve, reject, _next, _throw, "throw", err); } _next(undefined); }); }; }

/**
 * 图片压缩方法 
 * 当quality在0.2~0.5之间，图片质量变化并不大，quality的值越小，压缩效率越可观
 * （也就是在0.2左右时，压缩图片可以最大化，同时并不对图片质量造成太大影响）
 * @param {string} file 文件
 * @param {Number} quality  0~1之间 
 */
function compressImg(file, quality) {
  if (file[0]) {
    return Promise.all(Array.from(file).map(e => compressImg(e, quality))); // 如果是 file 数组返回 Promise 数组
  } else {
    return new Promise(resolve => {
      const reader = new FileReader(); // 创建 FileReader

      reader.onload = ({
        target: {
          result: src
        }
      }) => {
        const image = new Image(); // 创建 img 元素

        image.onload = _asyncToGenerator(function* () {
          const canvas = document.createElement('canvas'); // 创建 canvas 元素

          canvas.width = image.width;
          canvas.height = image.height;
          canvas.getContext('2d').drawImage(image, 0, 0, image.width, image.height); // 绘制 canvas

          const canvasURL = canvas.toDataURL('image/jpeg', quality);
          const buffer = atob(canvasURL.split(',')[1]);
          let length = buffer.length;
          const bufferArray = new Uint8Array(new ArrayBuffer(length));

          while (length--) {
            bufferArray[length] = buffer.charCodeAt(length);
          }

          const miniFile = new File([bufferArray], file.name, {
            type: 'image/jpeg'
          });
          resolve({
            file: miniFile,
            origin: file,
            beforeSrc: src,
            afterSrc: canvasURL,
            beforeKB: Number((file.size / 1024).toFixed(2)),
            afterKB: Number((miniFile.size / 1024).toFixed(2))
          });
        });
        image.src = src;
      };

      reader.readAsDataURL(file);
    });
  }
}